package imsdk

import (
	"encoding/json"
	"fmt"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/http"
)

type (
	MemberNumResp struct {
		ActionStatus string `json:"ActionStatus"`
		ErrorInfo    string `json:"ErrorInfo"`
		ErrorCode    int    `json:"ErrorCode"`
		MemberNum    int    `json:"OnlineMemberNum"`
	}
)

func (s sdk) MemberNum(groupId string) (num int, err error) {
	var resp MemberNumResp
	url := s.BuildRestfulUrl("/group_open_http_svc/get_online_member_num")

	body := fmt.Sprintf(`{"GroupId":"%s"}`, groupId)

	r, err := http.HttpPost(url, body, connTimeoutMs, serveTimeoutMs, "application/json")
	if err != nil {
		global.Log.Errorf("MemberNum error: %v, result: %s", err, r)
		return -1, err
	}

	err = json.Unmarshal(r, &resp)
	if err != nil {
		global.Log.Errorf("MemberNum Unmarshal error: %v, result: %s", err, r)
		return -1, err
	}

	return resp.MemberNum, nil
}
